package com.suyang.syt.controller;

import java.io.IOException;
import java.io.OutputStream;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.suyang.syt.model.User;
import com.suyang.syt.model.dto.AjaxObj;
import com.suyang.syt.model.dto.Captcha;
import com.suyang.syt.service.interfaces.LoginService;
import com.suyang.syt.service.interfaces.WeixinQRcodeService;
import com.suyang.syt.util.EmptyUtil;
import com.suyang.syt.util.StringUtil;

@Controller
public class LoginController {
	@Autowired
	private LoginService loginService;
	@Autowired
	private WeixinQRcodeService weixinQRcodeService;

	@RequestMapping(value = "/")
	public String index() {
		return "login";
	}

	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String login() {
		return "login";
	}

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj login(HttpServletRequest request, String username, String password, String vcode) {
		try {
			HttpSession session = request.getSession();
			if (EmptyUtil.isEmpty(vcode)) {
				return new AjaxObj(0, "验证码不能为空!");
			}
			if (!vcode.equalsIgnoreCase((String) session.getAttribute("vcodeCheckCode"))) {
				return new AjaxObj(0, "验证码错误!");
			}
			return loginService.updateLogin(username, password, session);
		} catch (Exception e) {
			return new AjaxObj(0, "您没有权限登陆!!!");
		}
	}

	@RequestMapping(value = "/addQrlogin", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj add(HttpServletRequest request) {
		try {
			return weixinQRcodeService.addQrlogin();
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

	@RequestMapping(value = "/qrlogin", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj qrlogin(HttpServletRequest request, int scene_id) {
		try {
			AjaxObj ajaxObj = weixinQRcodeService.updateQrlogin(scene_id);
			if (ajaxObj != null && ajaxObj.getResult() == 1) {
				loginService.longinHandle((User) ajaxObj.getObj(), request.getSession());
			}
			return ajaxObj;
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

	@RequestMapping(value = "/addQrpasswd", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj addQrpasswd(HttpServletRequest request) {
		try {
			return weixinQRcodeService.addQrpasswd();
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

	@RequestMapping(value = "/logout", method = RequestMethod.GET)
	public String logout(HttpServletRequest request) {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("loginUser");
		if (user != null && user.getId() > 0) {
			session.setAttribute("loginUser", null);
		}
		session.invalidate();
		return "redirect:/web/index.html";
	}

	@RequestMapping("/drawCheckCode")
	public void drawCheckCode(HttpServletRequest request, HttpServletResponse response) {
		OutputStream os = null;
		try {
			HttpSession session = request.getSession();
			response.setContentType("image/jpg");
			int width = 120;
			int height = 44;
			Captcha c = Captcha.getInstance();
			c.setCaptcha(width, height);
			String checkcode = c.generateCheckcode();
			session.setAttribute("vcodeCheckCode", checkcode);
			os = response.getOutputStream();
			ImageIO.write(c.generateCheckImg(checkcode), "jpg", os);
			os.flush();
			os.close();
		} catch (IOException e) {
		} finally {
			if (os != null) {
				try {
					os.flush();
					os.close();
				} catch (IOException e) {

				}
			}
		}
	}

}